package com.ysu.yybk.listener;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.iai.v20180301.IaiClient;
import com.tencentcloudapi.iai.v20180301.models.CompareFaceRequest;
import com.tencentcloudapi.iai.v20180301.models.CompareFaceResponse;
import com.ysu.yybk.entity.checkin.CheckinStudent;
import com.ysu.yybk.factory.TencentFactory;
import com.ysu.yybk.form.checkin.StudentSubmit;
import com.ysu.yybk.mapper.checkin.CheckinStudentMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author ：lhw
 * @date ：Created in 2019/11/26 12:37
 */
@Component
public class CheckinListener {
    private static final Logger logger = LoggerFactory.getLogger(CheckinListener.class);

    @Autowired
    ObjectMapper objectMapper;

    @Autowired
    CheckinStudentMapper checkinStudentMapper;

    @RabbitListener(queuesToDeclare = {
            @Queue(value = "checkinQueue")
    })
    private void checkin(String message){

        try {
            StudentSubmit studentSubmit = null;
            try {
                studentSubmit = objectMapper.readValue(message, StudentSubmit.class);
            } catch (IOException e) {
                logger.error(e.getMessage(),e);
            }

            if (studentSubmit == null) {
                return;
            }

            CompareFaceResponse resp = null;

            //拼接规则见yybk-cos说明文档
            String t1 = studentSubmit.getStudentId().substring(0, 4);
            String t2 = studentSubmit.getStudentId().substring(4, 8);
            String urlA = "https://res.ysucloud.com/checkin/" + "original/" + t1 + "/" + t2 + "/" + studentSubmit.getStudentId() + ".jpg";
            String urlB = "https://res.ysucloud.com/checkin/" + "latest/" + t1 + "/" + t2 + "/" + studentSubmit.getStudentId() + studentSubmit.getTimestamp() + ".jpg";

            try {
                IaiClient client = TencentFactory.getIaiClient();
                // 实例化一个请求对象
                CompareFaceRequest req = new CompareFaceRequest();

                req.setUrlA(urlA);
                req.setUrlB(urlB);

                // 通过client对象调用想要访问的接口，需要传入请求对象
                resp = client.CompareFace(req);
            } catch (TencentCloudSDKException e) {
                logger.error(e.getMessage(),e);
            }

            if (resp != null) {
                CheckinStudent checkinStudent = checkinStudentMapper.selectOne(new QueryWrapper<CheckinStudent>()
                        .eq("checkin_id", studentSubmit.getCheckinId())
                        .eq("student_id", studentSubmit.getStudentId())
                );

                checkinStudent.setSsim(resp.getScore().doubleValue());

                checkinStudentMapper.update(checkinStudent, new QueryWrapper<CheckinStudent>()
                        .eq("checkin_id", studentSubmit.getCheckinId())
                        .eq("student_id", studentSubmit.getStudentId())
                );
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
    }
}
